﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;

namespace MD5Sum
{
    class HashGenerator
    {
        private string md5File = "md5sum";
        List<CompareItem> data;
        private bool stop = false;

        public bool Stop { get => stop; set => stop = value; }
        public List<CompareItem> generate()
        {
            data = new List<CompareItem>();
            string[] files = Directory.GetFiles("./");
            if (File.Exists(md5File))
            {
                File.Delete(md5File);
            }


            foreach (string file in files)
            {
                string fileName = Path.GetFileName(file);
                string extension = Path.GetExtension(file);
                if (fileName.EndsWith("exe", StringComparison.OrdinalIgnoreCase) || fileName.Equals(md5File))
                {
                    continue;
                }
                string md5 = getMd5(file);
                data.Add(new CompareItem(fileName, md5, "", ""));

            }
            return data;
        }

        private string getMd5(string filePath)
        {
            using (var md5 = MD5.Create())
            {
                if (File.Exists(filePath))
                {
                    using (var stream = File.OpenRead(filePath))
                    {
                        var hash = md5.ComputeHash(stream);
                        return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
                    }
                }
                else
                {
                    return "File Not Exists!";
                }
            }
        }

        public List<CompareItem> compare()
        {
            //Thread.Sleep(5000);
            List<CompareItem> result = new List<CompareItem>();
            if (File.Exists("md5sum"))
            {
                string[] lines = File.ReadAllLines("md5sum");
                foreach (string line in lines)
                {
                    if (stop)
                    {
                        stop = false;
                        break;
                    }
                    if (line.Contains("\t*"))
                    {
                        string[] parts = line.Split("\t*".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                        if (parts.Length < 2) break;
                        string p1 = parts[0];
                        string p2 = parts[1];
                        string md5Hash = getMd5(p2);
                        CompareItem item = new CompareItem(p2, p1, md5Hash, "Yes");
                        if (p1.Equals(md5Hash, StringComparison.OrdinalIgnoreCase))
                        {
                            result.Add(item);
                        }
                        else
                        {
                            item.IsMatched = "No";
                            result.Add(item);
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                return result;
            }
            return null;
        }

    }
}
